<?php

namespace Management\Controller;

use Think\Controller;

class UserController extends BaseController {

	

	protected $m = NULL;

	protected $md = NULL;

	protected $m_group = NULL;

	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('user');	

		$this->md = D('Management/User');

		$this->m_group = M('user_group');

		$this->ld = D('UserLogDetail');	

	}

	







    public function index(){

		

		$group_id = $this->md->getAuthInfo('group_id');

		

		

		$dataList = $this->md->where('group_id > '.$group_id)->order('group_id asc , id asc')->select();

		$this->assign('dataList',$dataList);

		

		$powerList = $this->m_group->where('id > '.$group_id)->order('id asc')->select();

		$this->assign('powerList',$powerList);

		

		

		

		$this->display('index');

    }

	

	

	

	public function edit(){

		

		$group_id = $this->md->getAuthInfo('group_id');

		

		$powerList = $this->m_group->where('id >'.$group_id)->order('id asc')->select();

		$this->assign('powerList',$powerList);

	

		$this->display('edit');

	}

	

	

	public function save(){

		

		

		$temp = $this->m->create();

		$temp['password'] = md5($temp['password']);

		$result = $this->m->add($temp);

		if($result){

			$this->ld->save($this->m_id,$this->m_name,'user',$temp['id']?'修改':'新增',$temp['id']?$temp['id']:$result);

			$this->success($GLOBALS['notice']['success'][0],U('User/index'));

			}

		else {$this->error($GLOBALS['notice']['error'][0]);}	

	}	

	

	

	public function del(){



		$id=I('id',0,'int');

		if($id===1)$this->error('超级管理员无法删除');

		

		if ($this->m->delete($id)){

			$this->ld->save($this->m_id,$this->m_name,'user','删除',$id);

			$this->success($GLOBALS['notice']['success'][1]);

		}else{

			$this->error($GLOBALS['notice']['error'][1]);

		}	

    }		

	

	

	

	

	

	

	public function ajaxCheckUsername(){

		

		$username = I('param','','string');

		

		$user_id = $this->md->where('username="'.$username.'"')->getField('id');

		

		

		if(!$user_id){

			echo '{

				"info":"账户名称通过",

				"status":"y"

			 }';

			exit;

		}else{

			echo '{

				"info":"账户名称重复",

				"status":"n"

			 }';

			exit;

		}

		

		

	}	

	



	

    public function password(){

		

		$this->display('password');

		

    }

	

	

	

    public function editPassword(){

		

		

		$oldPass = I('oldpwd','','string');

		$newPass = I('newpwd','','string');

		$chkPass = I('chkpwd','','string');

		

		$user_id = $this->md->getAuthInfo('id');

		

		$password = $this->md->where('id="'.$user_id.'"')->getField('password');

		

		////-----验证阶段-----////

		if(empty($oldPass))$this->error('原始密码不能为空');

		if(empty($newPass))$this->error('新密码不能为空');

		

		

		if(md5($oldPass)!=$password)$this->error('原始密码不正确');

		if($newPass!=$chkPass)$this->error('新密码输入不一致，请重新输入');

		

		if($this->md->modifyPwd($newPass)){

			$this->assign('message','密码修改完毕，请重新登录');

			$this->display('success_jump');

		}else{

			$this->error('未知错误，请重新操作');

		}

		

		

    }	

	



    public function adminEditPass(){

		

		

		

		$admin_id = D('user')->getAuthInfo('id');	

		if($admin_id!=1)$this->error('非法操作');

		

		$user_id = I('id',0,'int');

		if(!$user_id)$this->error('非法操作');

		$this->assign('user_id',$user_id);

		

		

		$this->display('admin_password');

		

    }

	

    public function adminSavePass(){

	

	

		

		$newPass = I('newpwd','','string');

		$chkPass = I('chkpwd','','string');

		

		$user_id = I('u_id',0,'int');;

		

		

		

		

		

		////-----验证阶段-----////

		if(empty($newPass))$this->error('新密码不能为空');

		

		

		if($newPass!=$chkPass)$this->error('新密码输入不一致，请重新输入');

		

		$info['user_id'] = $user_id;

		$info['password'] = $newPass;

		

		if($this->md->modifyManagePwd($info)){

			$this->success('密码修改完毕',U('User/index'));

		}else{

			$this->error('未知错误，请重新操作');

		}

		

		

    }		

	

	

	

	

	public function ajaxCheckUserPass(){

		

		

		$oldPass = I('param','','string');

		$user_id = $this->md->getAuthInfo('id');

		

		$password = $this->md->where('id="'.$user_id.'"')->getField('password');

		

		

		if(md5($oldPass)==$password){

			echo '{

				"info":"原始密码验证通过",

				"status":"y"

			 }';

			exit;

		}else{

			echo '{

				"info":"原始密码错误",

				"status":"n"

			 }';

			exit;

		}

		

		

	}

	

	

	public function ajaxChangeLanguage(){

		$langVal = I('val','cn');

		$result = cookie( "AUTH_USER_LANG", $langVal );

		echo "success";exit;

	}

	

	

	

	

	

	

	

	

	public function power(){

	

		

		$group_id = $this->md->getAuthInfo('group_id');

		

		

		$dataList = $this->m_group->where('id >'.$group_id)->order('id asc')->select();

		$this->assign('dataList',$dataList);

		$this->display('power');	

	

	}

	

	

	

	

	

	public function powerEdit(){

	

		

		

		$group_id = $this->md->getAuthInfo('group_id');

		$group_keys = $this->m_group->where('id='.$group_id)->getField('group_keys');

		

		

		

		$id = I('id',0,'int');

		$dataInfo = $this->m_group->find($id);

		$this->assign('dataInfo',$dataInfo);	

	



		$subNav = C('NAV');

		$moduleList = D('Base')->listModules();

		

		foreach($moduleList as $item){

			$itemNav = $this->getModuleNav($item);

			if($itemNav){

				$subNav[$item."ExtraManage"] =$itemNav;

			}

		}

		$this->assign('subNav',$subNav);		

	

	

		$this->display('power_edit');

	}

		

	

	

	

	

	

	public function powerSave(){

	

		$temp = $this->m_group->create();



		$temp['group_keys'] = implode(',',$temp['group_keys']);



		if($temp['id']){

			$this->m_group->save($temp);

			$this->ld->save($this->m_id,$this->m_name,'user_group',$temp['id']?'修改':'新增',$temp['id']?$temp['id']:$result); 

			$this->success($GLOBALS['notice']['success'][2],U('User/power'));

		}else{

		    $result = $this->m_group->add($temp);

			if($result)

			{

				$this->ld->save($this->m_id,$this->m_name,'user_group',$temp['id']?'修改':'新增',$temp['id']?$temp['id']:$result); 

				$this->success('增加完毕',U('User/power'));

			}

			else {

				$this->success('增加失败');

			}

		}	



	

	}

			

	

	

	

	public function powerDel(){



		$id = I('id',0,'int');

		

		if ($this->m_group->delete($id)){

			$this->ld->save($this->m_id,$this->m_name,'user_group','删除',$id);

			$this->success($GLOBALS['notice']['success'][1]);

		}else{

			$this->error($GLOBALS['notice']['error'][1]);

		}	

    }		

		

	

	

	public function ajaxSelectUserPower(){

		

		

		$powerVal = I('val',0,'int');

		

		if(!$powerVal){

			echo "error";exit;

		}

		

		

		$mid = I('mid');

		

		$temp['id'] = $mid;

		$temp['group_id'] = $powerVal;

		$this->m->save($temp);

		echo "success";exit;

	}	

	

	

	public function database(){

		

		$this->display('database');

		

	}	



	public function getModuleNav($moduleName){

		$folderPath = dirname(__FILE__)."/../Conf/ModuleNav/";

		$fileName = strtolower($moduleName)."_nav.php"; 

		$resultNav = $this->readNav($folderPath.$fileName);

		return $resultNav;

	}

	

	private function readNav($filepath){

		$itemStr = str_replace(array('<?php','?>'),'',file_get_contents($filepath));

		$itemStr = mb_convert_encoding($itemStr,'UTF-8',"auto");

		return eval($itemStr);

	}



}